#include "NetworkManager.h"

namespace mpgl_engine{ namespace application{ namespace net {

	NetworkManager::NetworkManager(GlConsole* console,
		TcpSessionType sessionType) :
		m_sessionType(sessionType), m_console(console)
	{
		m_sessionType = TcpSessionType::SERVER;
#if (NETWORK_MANAGER_DEBUG >= 2)
		if (m_console != NULL){
			string msg = NetworkDefines::CONSOLE_MSG_PREFIX;
			msg += " Constructed ";
			m_console->WriteMessage(msg);
		}
#endif
	}

	/*---------------------------------------------------
	------------------------------------------------------*/

		NetworkManager::~NetworkManager()
		{
#if (NETWORK_MANAGER_DEBUG >= 2)
			if (m_console != NULL){
				string msg = NetworkDefines::CONSOLE_MSG_PREFIX;
				msg += " Destructing ";
				m_console->WriteMessage(msg);
			}
#endif
			if (m_session != NULL)
			{
				delete m_session;
			}
			m_session = NULL;
#if (NETWORK_MANAGER_DEBUG >= 2)
			if (m_console != NULL){
				string msg = NetworkDefines::CONSOLE_MSG_PREFIX;
				msg += " Destructed ";
				m_console->WriteMessage(msg);
			}
#endif
		}

		/*---------------------------------------------------
		------------------------------------------------------*/

		void NetworkManager::Initialize()
		{
#if NETWORK_MANAGER_DEBUG
			if (m_console != NULL){
				string msg = NetworkDefines::CONSOLE_MSG_PREFIX;
				msg += "NetworkManager -> Initializing";
				m_console->WriteMessage(msg);
			}
#endif

			try
			{
				m_ioService = new boost::asio::io_service();
#if NETWORK_MANAGER_DEBUG
				if (m_console != NULL){
					string msg = NetworkDefines::CONSOLE_MSG_PREFIX;
					msg += "NetworkManager :: boost::asio::io_service -> Initialized";
					m_console->WriteMessage(msg);
				}
#endif
				bool ss = false;
				if (ss)
				{
					m_session = new TcpServer(m_console, m_ioService, 8);
					m_session->Initialize();
					((TcpServer*)m_session)->StartAccept();
				}
				else
				{
					//create test server
					m_session = new TcpClient(m_console, m_ioService);
					m_session->Initialize();
					((TcpClient*)m_session)->Connect();
				}
				m_initialized = true;



			}
			catch (...)
			{
#if NETWORK_MANAGER_DEBUG
				if (m_console != NULL){
					string err = NetworkDefines::CONSOLE_ERR_PREFIX;
					err += "NetworkManager :: boost::asio::io_service -> Initialize Failed!";
					m_console->WriteError(err);
				}
#endif
			}

#if NETWORK_MANAGER_DEBUG
			if (m_console != NULL){
				string msg = NetworkDefines::CONSOLE_MSG_PREFIX;
				msg += "NetworkManager -> Finished Initializing";
				m_console->WriteMessage(msg);
			}
#endif
		}

		/*---------------------------------------------------
		------------------------------------------------------*/

		void NetworkManager::Uninitialize()
		{
			m_initialized = false;


#if (NETWORK_MANAGER_DEBUG >= 1)
			if (m_console != NULL){
				string msg = NetworkDefines::CONSOLE_MSG_PREFIX;
				msg += "NetworkManager -> Uninitialized";
				m_console->WriteMessage(msg);
			}
#endif
		}

		/*---------------------------------------------------
		------------------------------------------------------*/

		void NetworkManager::Update()
		{
			// May be ran from separate thread!

			//Update IO Service
			if (m_ioService != NULL)
			{
				m_ioService->poll();
			}
			//Update Session
			if (m_session != NULL)
			{
				m_session->Update();
			}
			Pause(10);
		}


}}}